package com.chaos.meta.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Slf4j
@Component
public class MailService {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${mail.sender}")
    private String SENDER;

    public void sendSimpleMail(String subject, String content, String[] cc, String[] bcc, String... to) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(SENDER);
        message.setTo(to);
        message.setCc(cc);
        message.setBcc(bcc);
        message.setSubject(subject);
        message.setText(content);
        try {
            mailSender.send(message);
            log.info("Simple Mail Send Successful");
        } catch (Exception e) {
            log.error("Simple Mail Send failed, the detail is :" + e);
        }
    }

    public void sendHtmlMail(String subject, String content, String[] cc, String[] bcc, String... to) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = buildMimeMessageHelper(subject, content, cc, bcc, message, to);
            mailSender.send(message);
            log.info("Html Mail Send Successful");
        } catch (MessagingException e) {
            log.error("Html Mail Send failed, the detail is :", e.getMessage());
        }

    }

    public void sendAttachmentsMail(String subject, String content, String filePath,
                                    String[] cc, String[] bcc, String... to) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = buildMimeMessageHelper(subject, content, cc, bcc, message, to);
            File file = new File(filePath);
            // get file path
            String fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("/") + 1);
            helper.addAttachment(fileName, file);
            mailSender.send(message);
            log.info("Attachments Mail Send Successful");
        } catch (MessagingException e) {
            log.error("Attachments Mail Send failed, the detail is :", e.getMessage());
        }
    }

    public void sendInlineResourceMail(String subject, String content, String rscPath,
                                       String rscId, String[] cc, String[] bcc, String... to) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = buildMimeMessageHelper(subject, content, cc, bcc, message, to);
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);
            mailSender.send(message);
            log.info("InlineResource Mail Send Successful");
        } catch (MessagingException e) {
            log.error("InlineResource Mail Send failed, the detail is :", e.getMessage());
        }
    }

    private MimeMessageHelper buildMimeMessageHelper(String subject, String content, String[] cc,
                                                     String[] bcc, MimeMessage message, String... to) throws MessagingException {
        // true means should create a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(SENDER);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setBcc(bcc);
        helper.setSubject(subject);
        helper.setText(content, true);
        return helper;
    }
}